package com.totoro.javacsv;

import com.csvreader.CsvReader;
import com.csvreader.CsvWriter;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.LineIterator;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;

/**
 * Created by Niki on 2018/3/23 17:14
 */
public class JavaCSVDemo {
    public static void main(String[] args) throws IOException {
        writeDemo();
    }

    private static void readDemop() {

        String filePath = "D:/people_demo.csv";
        CsvReader csvReader = null;
        try {
            csvReader = new CsvReader(filePath, ',', Charset.forName("GBK"));
            csvReader.readHeaders();
            while (csvReader.readRecord()) {
                String rr = csvReader.getRawRecord();
                System.out.println(rr);
                String r_2 = csvReader.get(2);
                r_2 = csvReader.get(0);
                r_2 = csvReader.get(1);
                System.out.println(r_2);
                String name = csvReader.get("姓名");
                System.out.println(name);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        } finally {
            csvReader.close();
        }

    }

    private static void writeDemo() throws IOException {
        String[] headers = { "姓名", "手机号", "邮箱账号", "所属组织名称全路径", "账号状态", "职位", "性别", "是否部门负责人", "企业工号", "人员排序" } ;
        String[] content = { "绿茂贵", "15216111369", "1542115261@qq.com", "研发中心", "1", "软件开发", "1", "0", "80002168", "1" } ;
        String filePath = "D:/people_demo.csv" ;
        File file = new File(filePath);
        if(!file.exists()){
            file.createNewFile();
        }
        CsvWriter csvWriter = new CsvWriter(filePath, ',', Charset.forName("UTF-8")) ;
        try {
            csvWriter.writeRecord(headers) ;
            csvWriter.writeRecord(content) ;
        } catch (IOException e) {
            e.printStackTrace() ;
        } finally {
            csvWriter.close() ;
        }
    }

    private static void readDemo() {
        String filePath = "D:/demo.csv" ;
        CsvReader csvReader = null ;
        try {
            csvReader = new CsvReader(filePath, ',', Charset.forName("GBK")) ;
            csvReader.readHeaders() ;
            while (csvReader.readRecord()) {
                String rr = csvReader.getRawRecord() ;
                System.out.println(rr) ;
                String r_2 = csvReader.get(2) ;
                r_2 = csvReader.get(0) ;
                r_2 = csvReader.get(1) ;
                System.out.println(r_2) ;
                String name = csvReader.get("姓名") ;
                System.out.println(name) ;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace() ;
        } catch (IOException e1) {
            e1.printStackTrace() ;
        } finally {
            csvReader.close() ;
        }
    }

    private static void readDemoByIO() {
        String filePath = "D:/demo.csv" ;
        LineIterator it = null ;
        try {
            it = FileUtils.lineIterator(new File(filePath), "GBK") ;
            while (it.hasNext()) {
                String line = it.nextLine() ;
                System.out.println(line) ;
            }
        } catch (IOException e) {
            e.printStackTrace() ;
        } finally {
            if (it != null) {
                LineIterator.closeQuietly(it) ;
            }
        }
    }
}
